﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TJADRICD.CollaborativeDesignPlugin.PreArchive
{
    class PreArchiveService
    {
        /// <summary>
        /// 动态创建归档目录
        /// dirctoryPath主要是由项目编号和子项目编号拼起的路径
        /// </summary>
        /// <param name="directoryPath"></param>
        public void makeDirectory(string directoryPath)
        {
            string dwgPath = directoryPath + "/dwg";
            string dwfPath = directoryPath + "/dwf";
            string pdfPath = directoryPath + "/pdf";
            string pltPath = directoryPath + "/plt";
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
                Directory.CreateDirectory(dwgPath);
                Directory.CreateDirectory(dwfPath);
                Directory.CreateDirectory(pdfPath);
                Directory.CreateDirectory(pltPath);
            }
        }
        /// <summary>
        /// 文件归档行函数
        /// </summary>
        /// <param name="sourceFilePath"></param>
        /// <param name="destFilePath"></param>
        public void preArchive(string sourceFilePath, string destFilePath)
        {
            string dwgSourcePath = destFilePath + "/dwg/";
            string dwfSourcePath = destFilePath + "/dwf/";
            string pdfSourcePath = destFilePath + "/pdf/";
            string pltSourcePath = destFilePath + "/plt/";


            if (Directory.Exists(sourceFilePath))
            {
                string[] dwgFiles = Directory.GetFiles(sourceFilePath);
                for (int i = 0; i < dwgFiles.Length; i++)
                {
                    string dwgFileName = dwgFiles[i].Substring(dwgFiles[i].LastIndexOf("\\") + 1);
                    File.Copy(dwgFiles[i], dwgSourcePath + "" + dwgFileName, true);
                }
            }


            if (Directory.Exists(sourceFilePath))
            {
                string[] dwfFiles = Directory.GetFiles(sourceFilePath);
                for (int i = 0; i < dwfFiles.Length; i++)
                {
                    string dwfFileName = dwfFiles[i].Substring(dwfFiles[i].LastIndexOf("\\") + 1);
                    File.Copy(dwfFiles[i], dwfSourcePath + "" + dwfFileName, true);
                }
            }

            if (Directory.Exists(sourceFilePath))
            {
                string[] pdfFiles = Directory.GetFiles(sourceFilePath);
                for (int i = 0; i < pdfFiles.Length; i++)
                {
                    string pdfFileName = pdfFiles[i].Substring(pdfFiles[i].LastIndexOf("\\") + 1);
                    File.Copy(pdfFiles[i], pdfSourcePath + "" + pdfFileName, true);
                }
            }

            if (Directory.Exists(sourceFilePath))
            {
                string[] pltFiles = Directory.GetFiles(sourceFilePath);
                for (int i = 0; i < pltFiles.Length; i++)
                {
                    string pltFileName = pltFiles[i].Substring(pltFiles[i].LastIndexOf("\\") + 1);
                    File.Copy(pltFiles[i], pltSourcePath + "" + pltFileName, true);
                }
            }
        }
        /// <summary>
        /// 组成预归档路径
        /// </summary>
        /// <param name="projectNo"></param>
        /// <param name="subProjectNo"></param>
        /// <returns></returns>
        public string getDirectoryPath(string projectNo, string subProjectNo)
        {
            return PreArchiveConfiguration.preArchiceRootPath + "/" + projectNo + "/" + subProjectNo;
        }
    }
}
